package su2010.puz;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JFrame;

import su2010.puz.util.ImageCls;

import com.pearsoneduc.ip.gui.ImageView;
import com.pearsoneduc.ip.io.ImageDecoder;
import com.pearsoneduc.ip.io.ImageDecoderException;
import com.pearsoneduc.ip.io.ImageEncoder;
import com.pearsoneduc.ip.io.ImageEncoderException;
import com.pearsoneduc.ip.io.ImageFile;

/**
 * Managing the image (reading, writing, displaying...)
 *
 */
public class ImageHelper{

	public static int LUMINANCE = 3;
	public static int RED = 0;
	public static int GREEN = 1;
	public static int BLUE = 2;
	public static int HUE = 4;
	public static int SATURATION = 5;
	public static int VALUE = 6;
	
	public static int HISTOGRAM = 10;
	public static int IMAGE = 11;
	
	public static ImageCls readImage(String path){
		try {
			ImageDecoder image = ImageFile.createImageDecoder(path);
			return new ImageCls(image.decodeAsBufferedImage());
		} catch (FileNotFoundException e) {
			System.out.println("File "+ path +" not found");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ImageDecoderException e) {
			System.out.println(e.getLocalizedMessage()+" "+path);
		}
		return null;
	}
	
	public static void writeImage(BufferedImage image, String path){

		try {
			ImageEncoder imageEncoder = ImageFile.createImageEncoder(path);
			imageEncoder.encode(image);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ImageEncoderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void showImage(BufferedImage image, String title){
		JFrame frame = new JFrame(title);
		ImageView imageView = new ImageView(image);
		frame.add(imageView);
		frame.pack();
		frame.setVisible(true);
	}
	
	public static void showImage(int x, int y, BufferedImage image, String title){
		JFrame frame = new JFrame(title);
		ImageView imageView = new ImageView(image);
		frame.add(imageView);
		frame.setLocation(x, y);
		frame.pack();
		frame.setVisible(true);
	}
	
	public static BufferedImage getBufferedImage(Image image){
		BufferedImage img = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_3BYTE_BGR);
		Graphics g = img.getGraphics();
		g.drawImage(image, 0, 0, null);
		g.dispose();
		return img;
	}
	
	public static BufferedImage getBufferedImage(Image image, int offset){
		int w = image.getWidth(null);
		int h = image.getHeight(null);
		BufferedImage img = new BufferedImage(w-offset*2, h-offset*2, BufferedImage.TYPE_3BYTE_BGR);
		Graphics g = img.getGraphics();
		g.drawImage(image, 0,0,w-offset*2,h-offset*2,offset,offset,w-offset*2-1,h-offset*2-1, null);
		g.dispose();
		return img;
	}
	
	public static String getColorName(int color_mode){
		String name = "";
		if(color_mode == ImageHelper.RED)
			name = "Red";
		if(color_mode == ImageHelper.BLUE)
			name = "Blue";
		if(color_mode == ImageHelper.GREEN)
			name = "Green";
		if(color_mode == ImageHelper.LUMINANCE)
			name = "Luminance";
		if(color_mode == ImageHelper.HUE)
			name = "Hue";
		if(color_mode == ImageHelper.SATURATION)
			name = "Saturation";
		if(color_mode == ImageHelper.VALUE)
			name = "Value";
		return name;
	}
	
	public static float normalize(float x, float fromLowerBoundary, float fromUpperBoundary, float toLowerBoundary, float toUpperBoundary){
		return toLowerBoundary + ((x - fromLowerBoundary)*(toUpperBoundary - toLowerBoundary))/(fromUpperBoundary-fromLowerBoundary);
	}

}
